package com.yc.cloud.admin.controller;

import com.yc.cloud.admin.dto.request.HwMonitoringPointCreateReqDTO;
import com.yc.cloud.admin.dto.request.HwMonitoringPointQueryReqDTO;
import com.yc.cloud.admin.dto.request.HwMonitoringPointUpdateReqDTO;
import com.yc.cloud.admin.dto.request.MonitoringPointBindCamerasReqDTO;
import com.yc.cloud.admin.service.HwMonitoringPointService;
import com.yc.cloud.admin.vo.HwMonitoringPointVo;
import com.yc.cloud.admin.vo.HwCameraVo;
import com.yc.cloud.common.basic.api.ResponseResult;
import com.yc.cloud.common.orm.CommonPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 监视点管理表 前端控制器
 * </p>
 *
 * @author Claude Code
 * @since 2025-09-29
 */
@Slf4j
@RestController
@RequestMapping("/hw-monitoring-points")
@Tag(name = "监视点管理", description = "监视点管理相关接口")
public class HwMonitoringPointController {

    @Resource
    private HwMonitoringPointService hwMonitoringPointService;

    @PostMapping
    @Operation(summary = "创建监视点", description = "创建新的监视点")
    public ResponseResult<Boolean> create(@Valid @RequestBody HwMonitoringPointCreateReqDTO createReqDTO) {
        Boolean result = hwMonitoringPointService.create(createReqDTO);
        return ResponseResult.success(result);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新监视点", description = "根据ID更新监视点信息")
    public ResponseResult<Boolean> update(
            @Parameter(description = "监视点ID") @PathVariable Long id,
            @Valid @RequestBody HwMonitoringPointUpdateReqDTO updateReqDTO) {
        Boolean result = hwMonitoringPointService.update(id, updateReqDTO);
        return ResponseResult.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除监视点", description = "根据ID删除监视点（逻辑删除）")
    public ResponseResult<Boolean> delete(@Parameter(description = "监视点ID") @PathVariable Long id) {
        Boolean result = hwMonitoringPointService.delete(id);
        return ResponseResult.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取监视点详情", description = "根据ID获取监视点详细信息")
    public ResponseResult<HwMonitoringPointVo> getDetail(@Parameter(description = "监视点ID") @PathVariable Long id) {
        HwMonitoringPointVo detail = hwMonitoringPointService.getDetail(id);
        return ResponseResult.success(detail);
    }

    @GetMapping
    @Operation(summary = "分页查询监视点", description = "根据条件分页查询监视点列表")
    public ResponseResult<CommonPage<HwMonitoringPointVo>> list(@Valid HwMonitoringPointQueryReqDTO queryReqDTO) {
        CommonPage<HwMonitoringPointVo> page = hwMonitoringPointService.list(queryReqDTO);
        return ResponseResult.success(page);
    }

    @GetMapping("/region/{regionId}")
    @Operation(summary = "根据区域ID获取监视点", description = "获取指定区域下的所有监视点")
    public ResponseResult<List<HwMonitoringPointVo>> listByRegionId(
            @Parameter(description = "区域ID") @PathVariable Long regionId) {
        List<HwMonitoringPointVo> list = hwMonitoringPointService.listByRegionId(regionId);
        return ResponseResult.success(list);
    }

    @GetMapping("/regions")
    @Operation(summary = "根据区域ID列表获取监视点", description = "获取指定区域列表下的所有监视点")
    public ResponseResult<List<HwMonitoringPointVo>> listByRegionIds(
            @Parameter(description = "区域ID列表，逗号分隔") @RequestParam List<Long> regionIds,
            @Parameter(description = "是否包含子区域") @RequestParam(defaultValue = "false") boolean includeChildren) {
        List<HwMonitoringPointVo> list = hwMonitoringPointService.listByRegionIds(regionIds, includeChildren);
        return ResponseResult.success(list);
    }

    @GetMapping("/point-type/{pointType}")
    @Operation(summary = "根据监视点类型获取监视点", description = "获取指定类型的所有监视点")
    public ResponseResult<List<HwMonitoringPointVo>> listByPointType(
            @Parameter(description = "监视点类型：1-重点区域, 2-一般区域, 3-出入口, 4-边界线") @PathVariable Integer pointType) {
        List<HwMonitoringPointVo> list = hwMonitoringPointService.listByPointType(pointType);
        return ResponseResult.success(list);
    }

    @GetMapping("/importance-level/{importanceLevel}")
    @Operation(summary = "根据重要程度获取监视点", description = "获取指定重要程度的所有监视点")
    public ResponseResult<List<HwMonitoringPointVo>> listByImportanceLevel(
            @Parameter(description = "重要程度：1-高, 2-中, 3-低") @PathVariable Integer importanceLevel) {
        List<HwMonitoringPointVo> list = hwMonitoringPointService.listByImportanceLevel(importanceLevel);
        return ResponseResult.success(list);
    }

    @PutMapping("/{id}/status")
    @Operation(summary = "更新监视点状态", description = "启用或禁用监视点")
    public ResponseResult<Boolean> updateStatus(
            @Parameter(description = "监视点ID") @PathVariable Long id,
            @Parameter(description = "状态：1-启用, 0-禁用") @RequestParam Integer status) {
        Boolean result = hwMonitoringPointService.updateStatus(id, status);
        return ResponseResult.success(result);
    }

    @GetMapping("/check-code")
    @Operation(summary = "检查监视点编码", description = "检查监视点编码是否已存在")
    public ResponseResult<Boolean> checkCode(
            @Parameter(description = "监视点编码") @RequestParam String code,
            @Parameter(description = "排除的ID（用于更新时检查）") @RequestParam(required = false) Long excludeId) {
        Boolean exists = hwMonitoringPointService.existsByCode(code, excludeId);
        return ResponseResult.success(exists);
    }

    @GetMapping("/enabled")
    @Operation(summary = "获取所有启用的监视点", description = "获取所有状态为启用的监视点列表")
    public ResponseResult<List<HwMonitoringPointVo>> listAllEnabled() {
        List<HwMonitoringPointVo> list = hwMonitoringPointService.listAllEnabled();
        return ResponseResult.success(list);
    }

    @GetMapping("/code/{code}")
    @Operation(summary = "根据编码获取监视点", description = "根据监视点编码获取监视点详情")
    public ResponseResult<HwMonitoringPointVo> getByCode(
            @Parameter(description = "监视点编码") @PathVariable String code) {
        HwMonitoringPointVo detail = hwMonitoringPointService.getByCode(code);
        return ResponseResult.success(detail);
    }

    @GetMapping("/region/{regionId}/count")
    @Operation(summary = "统计区域监视点数量", description = "统计指定区域下的监视点数量")
    public ResponseResult<Long> countByRegionId(
            @Parameter(description = "区域ID") @PathVariable Long regionId) {
        Long count = hwMonitoringPointService.countByRegionId(regionId);
        return ResponseResult.success(count);
    }

    @PostMapping("/{monitoringPointId}/cameras")
    @Operation(summary = "批量绑定相机到监视点", description = "将多个相机批量绑定到指定监视点（会先解绑该监视点原有的所有相机）")
    public ResponseResult<Integer> bindCameras(
            @Parameter(description = "监视点ID") @PathVariable Long monitoringPointId,
            @Valid @RequestBody MonitoringPointBindCamerasReqDTO bindReqDTO) {
        Integer count = hwMonitoringPointService.bindCameras(monitoringPointId, bindReqDTO.getCameraIds());
        return ResponseResult.success(count);
    }

    @GetMapping("/{monitoringPointId}/cameras")
    @Operation(summary = "查询监视点绑定的相机列表", description = "获取指定监视点绑定的所有相机")
    public ResponseResult<List<HwCameraVo>> listBoundCameras(
            @Parameter(description = "监视点ID") @PathVariable Long monitoringPointId) {
        List<HwCameraVo> cameras = hwMonitoringPointService.listBoundCameras(monitoringPointId);
        return ResponseResult.success(cameras);
    }

    @GetMapping("/{monitoringPointId}/bindable-cameras")
    @Operation(summary = "查询监视点可绑定的相机列表", 
               description = "获取可以绑定到该监视点的相机（包括未绑定任何监视点的相机 + 已绑定到当前监视点的相机，已绑定的排在前面）")
    public ResponseResult<List<HwCameraVo>> listBindableCameras(
            @Parameter(description = "监视点ID") @PathVariable Long monitoringPointId) {
        List<HwCameraVo> cameras = hwMonitoringPointService.listBindableCameras(monitoringPointId);
        return ResponseResult.success(cameras);
    }

    @GetMapping("/{monitoringPointId}/cameras/count")
    @Operation(summary = "统计监视点绑定的相机数量", description = "统计指定监视点绑定的相机数量")
    public ResponseResult<Long> countBoundCameras(
            @Parameter(description = "监视点ID") @PathVariable Long monitoringPointId) {
        Long count = hwMonitoringPointService.countBoundCameras(monitoringPointId);
        return ResponseResult.success(count);
    }
}